package models.data;

import models.general.vKennariHoparOnn;
import models.general.vNemandiAfangarOnn;
import org.joda.time.DateTime;
import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: olgak
 * Date: 3.5.2013
 * Time: 10:39
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Heimavinna extends Model {
    @Id
    @Constraints.Required
    private Integer id;
    @Constraints.Required
    private Date dagsetning;
    /*TIMESLOT
    1=8:30-9:15
    2=9:20-10:05
    3=10:20-11:05
    4=11:10-11:55
    5=12:20-13:05
    6=13:10-13:55
    7=14:00-14:45
    8=14:55-15:40
 */
    @Constraints.Required
    private Integer timeslot;
    @Constraints.Required
    private String afangi_id;
    @Constraints.Required
    private String hopur_nafn;
    private String texti;

    public Date getDagsetning() {
        return dagsetning;
    }

    public void setDagsetning(Date dagsetning) {
        this.dagsetning = dagsetning;
    }

    public Integer getTimeslot() {
        return timeslot;
    }

    public void setTimeslot(Integer timeslot) {
        this.timeslot = timeslot;
    }

    public String getAfangi_id() {
        return afangi_id;
    }

    public void setAfangi_id(String afangi_id) {
        this.afangi_id = afangi_id;
    }

    public String getHopur_nafn() {
        return hopur_nafn;
    }

    public void setHopur_nafn(String hopur_nafn) {
        this.hopur_nafn = hopur_nafn;
    }

    public String getTexti() {
        return texti;
    }

    public void setTexti(String texti) {
        this.texti = texti;
    }

    public Heimavinna(Integer timeslot, String hopur_nafn, String afangi_id, Date dagsetning) {
        this.timeslot = timeslot;
        this.hopur_nafn = hopur_nafn;
        this.afangi_id = afangi_id;
        Date dateWithoutTime = new Date();
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            dateWithoutTime = sdf.parse(sdf.format(dagsetning));
        }
        catch(ParseException ex) {
                //donothing
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        this.dagsetning = dateWithoutTime;
        this.id = createId();
    }
    public static Heimavinna createHomework(Integer timeslot, String hopur_nafn, String afangi_id, Date dagsetning, String texti){
        Heimavinna heimavinna = new Heimavinna(timeslot,hopur_nafn, afangi_id,dagsetning);
        heimavinna.setTexti(texti);
        heimavinna.save();
        return heimavinna;
    }

    public Heimavinna() {
    }

    public Heimavinna(int timeslot, Date date,String texti) {
        this.timeslot=timeslot;
        this.dagsetning=date;
        this.texti=texti;
    }

    //send in a date and we get all the homework registered for this day
    public static List<Heimavinna> getAllHeimavinnaForDay(Date date){
        return find.where().eq("dagsetning", date).findList();
    }
    public static List<Heimavinna> getAllHeimavinnaForWeek(List<Date> dates,String hopur_nafn, String afangi_id){
        List<Heimavinna> heimavinna = new ArrayList<Heimavinna>();
        for(Date day:dates){
            List<Heimavinna> singleDay =  find.where().eq("dagsetning", day).where().eq("hopur_nafn", hopur_nafn).where().eq("afangi_id", afangi_id).findList();
            for(Heimavinna record : singleDay){
                heimavinna.add(record);
            }
        }
        return heimavinna;
    }

    //List<List<Stundatoflur_NK>> getStundatafla(String user_id, boolean isStudent){
    public static List<List<Heimavinna>> getStudentHomeworkWeek(List<vNemandiAfangarOnn> courses){
        List<Heimavinna> monday = new ArrayList<Heimavinna>(); //the homework for monday
        List<Heimavinna> tuesday = new ArrayList<Heimavinna>();   //homework for tuesdday
        List<Heimavinna> wednesday = new ArrayList<Heimavinna>(); //homework for wednesday
        List<Heimavinna> thursday = new ArrayList<Heimavinna>();   //homework for thursday
        List<Heimavinna> friday = new ArrayList<Heimavinna>();  //schedule for friday
        List<List<Heimavinna>> allWeek= new ArrayList<List<Heimavinna>>(); //the returnlist which will have all the days together, list of the days
        Date now = new Date();
        Date formattedNow = new Date();
        Date Manudagur = new Date();
        Date Thridjudagur=new Date();
        Date Midvikudagur=new Date();
        Date Fimmtudagur=new Date();
        Date Fostudagur=new Date();

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            formattedNow = sdf.parse(sdf.format(now));
        }
        catch(ParseException ex) {
            //donothing
        }
        //initialize all the lists with the timestamp of now and all the timeslots for the week
        for(int i=0;i<8;i++){
            monday.add(new Heimavinna(i+1,formattedNow,""));
            tuesday.add(new Heimavinna(i + 1, formattedNow,""));
            wednesday.add(new Heimavinna(i+1,formattedNow,""));
            thursday.add(new Heimavinna(i+1,formattedNow,""));
            friday.add(new Heimavinna(i+1,formattedNow,""));
        }
        Calendar newC = new GregorianCalendar();
        newC.setTime(now);
        newC.setFirstDayOfWeek(Calendar.MONDAY);
        int today = newC.get(Calendar.DAY_OF_WEEK);
        newC.add(Calendar.DAY_OF_WEEK,-today+Calendar.MONDAY); //first monday of the week
        List<Date> week = new ArrayList<Date>();
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Manudagur = sdf.parse(sdf.format(newC.getTime())); //we don't want to the date to have any time associated with it because only the dates matter, not the time
            newC.add(Calendar.DATE, 1);
            Thridjudagur= sdf.parse(sdf.format(newC.getTime()));
            newC.add(Calendar.DATE, 1);
            Midvikudagur= sdf.parse(sdf.format(newC.getTime()));
            newC.add(Calendar.DATE, 1);
            Fimmtudagur= sdf.parse(sdf.format(newC.getTime()));
            newC.add(Calendar.DATE, 1);
            Fostudagur= sdf.parse(sdf.format(newC.getTime()));
        }
        catch(ParseException ex) {
            //donothing
        }
        week.add(Manudagur);
        week.add(Thridjudagur);  //tuesday
        week.add(Midvikudagur);  //wednesday
        week.add(Fimmtudagur);  //wednesday
        week.add(Fostudagur);  //friday
        //Everything above here is just getting the right dates to send to the function to fetch the homework from the DB

        for(vNemandiAfangarOnn course:courses){
            List<Heimavinna> weekHeimavinna = getAllHeimavinnaForWeek(week,course.hopur, Integer.toString(course.afangi_id));

            for (Heimavinna single:weekHeimavinna){
                        if(single.getDagsetning().equals(Manudagur)){
                            monday.set(single.getTimeslot()-1, single);
                        }
                        if(single.getDagsetning().equals(Thridjudagur)){
                            tuesday.set(single.getTimeslot() - 9, single);
                        }
                        if(single.getDagsetning().equals(Midvikudagur)){
                            wednesday.set(single.getTimeslot() - 17, single);
                        }
                        if(single.getDagsetning().equals(Fimmtudagur)){
                            //if(single.getTimeslot()-1==)
                            thursday.set(single.getTimeslot()-25, single);
                        }
                        if(single.getDagsetning().equals(Fostudagur)){
                            friday.set(single.getTimeslot()-33, single);
                        }
                    }
        }
        allWeek.add(monday);
        allWeek.add(tuesday);
        allWeek.add(wednesday);
        allWeek.add(thursday);
        allWeek.add(friday);
        return allWeek;
    }

    //List<List<Stundatoflur_NK>> getStundatafla(String user_id, boolean isStudent){
    public static List<List<Heimavinna>> getTeacherHomeworkWeek(List<vKennariHoparOnn> courses){
        List<Heimavinna> monday = new ArrayList<Heimavinna>(); //the homework for monday
        List<Heimavinna> tuesday = new ArrayList<Heimavinna>();   //homework for tuesdday
        List<Heimavinna> wednesday = new ArrayList<Heimavinna>(); //homework for wednesday
        List<Heimavinna> thursday = new ArrayList<Heimavinna>();   //homework for thursday
        List<Heimavinna> friday = new ArrayList<Heimavinna>();  //schedule for friday
        List<List<Heimavinna>> allWeek= new ArrayList<List<Heimavinna>>(); //the returnlist which will have all the days together, list of the days
        Date now = new Date();
        Date formattedNow = new Date();
        Date Manudagur = new Date();
        Date Thridjudagur=new Date();
        Date Midvikudagur=new Date();
        Date Fimmtudagur=new Date();
        Date Fostudagur=new Date();

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            formattedNow = sdf.parse(sdf.format(now));
        }
        catch(ParseException ex) {
            //donothing
        }
        //initialize all the lists with the timestamp of now and all the timeslots for the week
        for(int i=0;i<8;i++){
            monday.add(new Heimavinna(i+1,formattedNow,""));
            tuesday.add(new Heimavinna(i + 1, formattedNow,""));
            wednesday.add(new Heimavinna(i+1,formattedNow,""));
            thursday.add(new Heimavinna(i+1,formattedNow,""));
            friday.add(new Heimavinna(i+1,formattedNow,""));
        }
        Calendar newC = new GregorianCalendar();
        newC.setTime(now);
        newC.setFirstDayOfWeek(Calendar.MONDAY);
        int today = newC.get(Calendar.DAY_OF_WEEK);
        newC.add(Calendar.DAY_OF_WEEK,-today+Calendar.MONDAY); //first monday of the week
        List<Date> week = new ArrayList<Date>();
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Manudagur = sdf.parse(sdf.format(newC.getTime())); //we don't want to the date to have any time associated with it because only the dates matter, not the time
            newC.add(Calendar.DATE, 1);
            Thridjudagur= sdf.parse(sdf.format(newC.getTime()));
            newC.add(Calendar.DATE, 1);
            Midvikudagur= sdf.parse(sdf.format(newC.getTime()));
            newC.add(Calendar.DATE, 1);
            Fimmtudagur= sdf.parse(sdf.format(newC.getTime()));
            newC.add(Calendar.DATE, 1);
            Fostudagur= sdf.parse(sdf.format(newC.getTime()));
        }
        catch(ParseException ex) {
            //donothing
        }
        week.add(Manudagur);
        week.add(Thridjudagur);  //tuesday
        week.add(Midvikudagur);  //wednesday
        week.add(Fimmtudagur);  //wednesday
        week.add(Fostudagur);  //friday
        //Everything above here is just getting the right dates to send to the function to fetch the homework from the DB

        for(vKennariHoparOnn course:courses){
            List<Heimavinna> weekHeimavinna = getAllHeimavinnaForWeek(week,course.hopur, Integer.toString(course.afangi_id));

            for (Heimavinna single:weekHeimavinna){
                if(single.getTexti()==null){
                    single.setTexti("");
                }
                if(single.getDagsetning().equals(Manudagur)){
                    monday.set(single.getTimeslot()-1, single);
                }
                if(single.getDagsetning().equals(Thridjudagur)){
                    tuesday.set(single.getTimeslot() - 9, single);
                }
                if(single.getDagsetning().equals(Midvikudagur)){
                    wednesday.set(single.getTimeslot() - 17, single);
                }
                if(single.getDagsetning().equals(Fimmtudagur)){
                    thursday.set(single.getTimeslot()-25, single);
                }
                if(single.getDagsetning().equals(Fostudagur)){
                    friday.set(single.getTimeslot()-33, single);
                }
            }
        }
        allWeek.add(monday);
        allWeek.add(tuesday);
        allWeek.add(wednesday);
        allWeek.add(thursday);
        allWeek.add(friday);
        return allWeek;
    }

    public static Finder<Integer,Heimavinna> find = new Finder<Integer,Heimavinna>(
            Integer.class, Heimavinna.class
    );

    public static Integer createId(){
        List<Object> heimavinna = Heimavinna.find.findIds();

        Integer max = 1;
        for(Object verk : heimavinna){
            if(Integer.valueOf(verk.toString()) > max) {
                max = Integer.valueOf(verk.toString());
            }
        }
        return (max+1);
    }
}
